package com.fqyd.campustransaction.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fqyd.campustransaction.model.Goods;
import com.fqyd.campustransaction.model.Task;
import com.fqyd.campustransaction.model.Type;
import com.fqyd.campustransaction.model.User;
import com.fqyd.campustransaction.utils.Tools;

/**
 * 管理员
 */
@Controller
@Scope(value = "prototype")
public class AdminController extends BaseController {

	@RequestMapping("admin_index")
	public String index() {
		return "admin/index";
	}

	@RequestMapping("admin_user_list")
	public String userList(HttpServletRequest request) {
		List<User> users = userService.getAll();
		request.setAttribute("users", users);
		return "admin/user_list";
	}

	@RequestMapping("admin_user_add")
	public String userAdd(HttpServletRequest request, RedirectAttributes attributes) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String mobile = request.getParameter("mobile");
		String address = request.getParameter("address");
		if (Tools.isEmpty(username) || Tools.isEmpty(password)) {
			return "admin/user_add";
		}
		User user = new User();
		user.setId(Tools.createID());
		user.setUsername(username);
		user.setPassword(password);
		user.setRole("普通学生");
		user.setMobile(mobile);
		user.setAddress(address);
		user.setCreateTime(new Date());
		if (userService.checkUsername(username)) {
			request.setAttribute("user", user);
			request.setAttribute("message", "操作失败：用户（" + user.getUsername() + "）已存在");
			return "admin/user_add";
		}
		userService.add(user);
		attributes.addFlashAttribute("message", "操作成功：新增用户（" + user.getUsername() + "）成功");
		return "redirect:admin_user_list.do";
	}

	@RequestMapping("admin_user_modify")
	public String userModify(HttpServletRequest request) {
		String userId = request.getParameter("user_id");
		if (Tools.isEmpty(userId)) {
			return "redirect:admin_user_list.do";
		}
		User user = userService.getById(userId);
		if (user == null) {
			return "redirect:admin_user_list.do";
		}
		request.setAttribute("user", user);
		return "admin/user_modify";
	}

	@RequestMapping("admin_user_modify_save")
	public String userModifySave(HttpServletRequest request, RedirectAttributes attributes) {
		String userId = request.getParameter("user_id");
		String mobile = request.getParameter("mobile");
		String address = request.getParameter("address");
		if (Tools.isEmpty(userId)) {
			return "redirect:admin_user_list.do";
		}
		User user = userService.getById(userId);
		if (user == null) {
			return "redirect:admin_user_list.do";
		}
		request.setAttribute("user", user);
		user.setMobile(mobile);
		user.setAddress(address);
		userService.update(user);
		attributes.addFlashAttribute("message", "操作成功：用户（" + user.getUsername() + "）个人资料已更新");
		return "redirect:admin_user_list.do";
	}

	@RequestMapping("admin_user_reset_password")
	public String userResetPassword(HttpServletRequest request, RedirectAttributes attributes) {
		String userId = request.getParameter("user_id");
		if (Tools.isEmpty(userId)) {
			return "redirect:admin_user_list.do";
		}
		User user = userService.getById(userId);
		if (user == null) {
			return "redirect:admin_user_list.do";
		}
		user.setPassword("123456");
		userService.update(user);
		attributes.addFlashAttribute("message", "操作成功：用户（" + user.getUsername() + "）密码已重置(初始化：123456)");
		return "redirect:admin_user_list.do";
	}

	@RequestMapping("admin_user_delete")
	public String userDelete(HttpServletRequest request, RedirectAttributes attributes) {
		String userId = request.getParameter("user_id");
		if (Tools.isEmpty(userId)) {
			return "redirect:admin_user_list.do";
		}
		User user = userService.getById(userId);
		if (user == null) {
			return "redirect:admin_user_list.do";
		}
		userService.delete(user.getId());
		attributes.addFlashAttribute("message", "操作成功：用户（" + user.getUsername() + "）已删除");
		return "redirect:admin_user_list.do";
	}

	@RequestMapping("admin_goods_audit")
	public String goodsAudit(HttpServletRequest request) {
		List<Goods> goodsList = goodsService.getByAudit();
		List<Type> types = typeService.getAll();
		request.setAttribute("goodsList", goodsList);
		request.setAttribute("types", types);
		return "admin/goods_audit";
	}

	@RequestMapping("admin_goods_show")
	public String goodsShow(HttpServletRequest request) {
		String goodsId = request.getParameter("goods_id");
		if (Tools.isEmpty(goodsId)) {
			return "redirect:admin_goods_audit.do";
		}
		Goods goods = goodsService.getById(goodsId);
		if (goods == null) {
			return "redirect:admin_goods_audit.do";
		}
		List<Type> types = typeService.getAll();
		request.setAttribute("types", types);
		request.setAttribute("goods", goods);
		return "admin/goods_show";
	}

	@RequestMapping("admin_goods_audit_result")
	public String goodsAuditResult(HttpServletRequest request, RedirectAttributes attributes) {
		String goodsId = request.getParameter("goods_id");
		String result = request.getParameter("result");
		if (Tools.isEmpty(goodsId) || Tools.isEmpty(result)) {
			return "redirect:admin_goods_audit.do";
		}
		Goods goods = goodsService.getById(goodsId);
		if (goods == null) {
			return "redirect:admin_goods_audit.do";
		}
		goods.setStatus(result);
		goodsService.update(goods);
		attributes.addFlashAttribute("message", "操作成功：商品（" + goods.getName() + "）已审核");
		return "redirect:admin_goods_audit.do";
	}

	@RequestMapping("admin_task_audit")
	public String taskAudit(HttpServletRequest request) {
		List<Task> tasks = taskService.getByAudit();
		List<Type> types = typeService.getAll();
		request.setAttribute("tasks", tasks);
		request.setAttribute("types", types);
		return "admin/task_audit";
	}

	@RequestMapping("admin_task_show")
	public String taskShow(HttpServletRequest request) {
		String taskId = request.getParameter("task_id");
		if (Tools.isEmpty(taskId)) {
			return "redirect:admin_task_audit.do";
		}
		Task task = taskService.getById(taskId);
		if (task == null) {
			return "redirect:admin_task_audit.do";
		}
		List<Type> types = typeService.getAll();
		request.setAttribute("types", types);
		request.setAttribute("task", task);
		return "admin/task_show";
	}

	@RequestMapping("admin_task_audit_result")
	public String taskAuditResult(HttpServletRequest request, RedirectAttributes attributes) {
		String taskId = request.getParameter("task_id");
		String result = request.getParameter("result");
		if (Tools.isEmpty(taskId) || Tools.isEmpty(result)) {
			return "redirect:admin_task_audit.do";
		}
		Task task = taskService.getById(taskId);
		if (task == null) {
			return "redirect:admin_task_audit.do";
		}
		task.setStatus(result);
		taskService.update(task);
		attributes.addFlashAttribute("message", "操作成功：任务（" + task.getName() + "）已审核");
		return "redirect:admin_task_audit.do";
	}
}
